package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.help.menu.InitMenuHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.pro.menu.pojo.vo.MenuInitVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @version v1.0
 * @des: 初始化呼叫中心菜单
 * @author: youli.chen
 * @date: $ $
 * @since v1.0
 */

@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/callCenter")
public class CallCenterController {

    @Resource
    private InitMenuHelp initMenuHelp;

    @RequestMapping(value = "/initMenu", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String info(@RequestBody @Valid MenuInitDTO menuInitDTO, BindingResult br) {
        XbbResponse<MenuInitVO> response = new XbbResponse<>();
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if (!Objects.equals(menuInitDTO.getAlias(), XbbRefTypeEnum.CALL_CENTER_DU_YAN.getAlias())
                        && !Objects.equals(menuInitDTO.getAlias(), XbbRefTypeEnum.CALL_CENTER_XBB.getAlias())) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100028);
                }
                this.initMenuHelp.menuInit(menuInitDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
